<script>
import modal from '@/common/js/modal.js';
export default {
	onShow() {
		this.$store.common.runBehind = false;
	},
	onHide() {
		this.$store.common.runBehind = true;
	},
	onLaunch() {
		//绑定自定义弹出消息
		uni.showConfirm = (op = {}) => {
			return new Promise((resolve, reject) => {
				const ssm = new modal({
					...op,
					$event: e => {
						if (e.res) {
							resolve(e);
						} else {
							reject(e);
						}
					}
				});
				ssm.show();
				uni.hideModal = () => {
					ssm.hide();
				};
			});
		};
		uni.showAlert = (op = {}) => {
			op.delCancel = true;
			return uni.showConfirm(op);
		};
		uni.showPrompt = (op = {}) => {
			op.confirmColor = op.confirmColor || '#6882b4';
			op.input = op.input || {};
			return uni.showConfirm(op);
		};
		//绑定推送消息
		plus.push.addEventListener('receive', data => {
			if (typeof data === 'object') {
				try {
					data = JSON.stringify(data);
				} catch (e) {
					data = data.toString();
				}
			}
			plus.push.createMessage(data, '123', {
				title: `收到消息`
			});
		});
		plus.push.addEventListener('click', msg => {
			if (msg.payload.type == 5) {
				plus.runtime.install(msg.payload.filePath);
			} else {
				const pages = getCurrentPages();
				for (let i = 0; i < pages.length; i++) {
					const page = pages[i];
					if (page.route.includes('/chat')) {
						page.$vm.reload(msg.payload.type, msg.payload.other);
						if (i < pages.length - 1) {
							uni.navigateBack({
								delta: pages.length - i - 1
							});
						}
						return;
					}
				}
				uni.navigateTo({
					url: `./chat?id=${msg.payload.other}&type=${msg.payload.type}`
				});
			}
		});
		//只能竖屏
		plus.screen.lockOrientation('portrait-primary');
		//获取系统信息
		let sysInfo = uni.getSystemInfoSync();
		this.$store.common.sysInfo = sysInfo; //保存系统信息
		this.$store.common.createDir('_doc/Voice/'); //创建录音文件夹
		this.$store.common.createDir('_doc/Download/'); //创建下载文件夹
		this.$store.common.createDir('_doc/ChatFile/'); //创建聊天文件夹
		uni.clientInfo = plus.push.getClientInfo();
		//获取设置的系统属性
		plus.runtime.getProperty(plus.runtime.appid, async info => {
			try {
				this.$store.common.version = info;
				//同步服务器时间
				this.$store.apis
					.getServerTime()
					.then(time => {
						this.$store.common.serverTime = time;
					})
					.catch(e => {
						console.log('服务器时间获取失败', e);
						this.$store.common.serverTime = this.$store.common.getChineseTime();
					});
				this.$store.apis
					.getRSAPublicKey()
					.then(key => {
						this.$store.common.publicKey = key;
					})
					.catch(e => {
						console.log('获取公钥失败', e);
					});
				//检查版本
				const { Success, Result: data } = await this.$store.apis.checkVersion({
					version: info.versionCode,
					phoneVersion: sysInfo.platform
				});
				if (!Success) {
					return;
				}
				this.$store.common.newVersion = data;
				let size = (data.FileSize / 1024 / 1024).toFixed(2) + 'MB';
				uni.showConfirm({
					content: `有更新版本，大小：${size},您是否要更新？`,
					confirmVal: '是',
					cancelVal: '否'
				}).then(() => {
					uni.navigateTo({
						url: '/pages/mine/checkVersion?update=true'
					});
				});
			} catch (e) {
				console.log('检查版本失败', e);
			}
		});
		//网络变化则重新连接一下
		uni.onNetworkStatusChange(e => {
			//如果没登录则不用管
			if (!this.$store.common.loginUser) {
				return;
			}
			if (e.isConnected) {
				this.$store.sock.initSocket();
			} else {
				this.$store.common.connected = false;
			}
		});
	}
};
</script>

<style>
@import url('common/css/main.css');
</style>
